﻿using LPLS.Commons.DBModule;
using LPLS.Commons.Enums;
using LPLS.DBEntities;
using LPLS.DBEntities.ConfigModels.DeviceInfoModels;
using LPLS.DBEntities.OEEModule.OEEModels.DataStatisticModels;
using LPLS.DBInterface.ConfigModels.DeviceInfoModels;
using LPLS.DBInterface.OEEModule.OEEModels.DataStaticsticModels;
using LPLS.DBServices.ConfigModels.DeviceInfoModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.DBServices.OEEModule.OEEModels.DataStaticsticModels
{
    public class CompentDataStatisticsService : DBBaseService, ICompentDataStatisticsService
    {


      private readonly IDeviceBaseInfoService _deviceBaseInfoService;
        public CompentDataStatisticsService(DBContext dbContext, IDeviceBaseInfoService deviceBaseInfoService) : base(dbContext)
        {
            _deviceBaseInfoService = deviceBaseInfoService;
        }   
        public List<CompentDataStatistics> GetData(DateTime startTime, DateTime endTime, Guid  deviceid)
        {
          var tempdata=Client.Queryable<CompentDataStatistics>()
                .Where(e => e.DeviceID == deviceid && e.StartTime >= startTime && e.StartTime <= endTime)
                .OrderBy(s => s.StartTime).ToList();
            /// 切换数据库
            TempDataDeal(tempdata,deviceid);
            return tempdata;
        }

        public List<CompentDataStatistics> GetData(DateTime startTime, DateTime endTime, Guid  deviceid, string tubeComponentCode)
        {
           var tempdata = Client.Queryable<CompentDataStatistics>()
                .Where(e => e.DeviceID == deviceid && e.StartTime >= startTime && e.StartTime <= endTime && e.ComponentCode == tubeComponentCode)
                .OrderBy(s => s.StartTime).ToList();
            /// 切换数据库
            TempDataDeal(tempdata, deviceid);
            return tempdata;
        }

        private void TempDataDeal(List<CompentDataStatistics> tempata,Guid  deviceid)
        {
           
            var devicemodel = _deviceBaseInfoService.GetData().Where(e=>e.ID==deviceid)?.FirstOrDefault()??null;

            if (devicemodel == null)
            {
                return;
            }
            var compnentlist = devicemodel.DeviceComponents;

            Parallel.For(0, tempata.Count, i =>
            {
                var compname= compnentlist.FirstOrDefault(x => x.ID == tempata[i].CompentId)?.ComponentName ?? "未知部件";
                var compenttypeenum = compnentlist.FirstOrDefault(x => x.ID == tempata[i].CompentId)?.ComponentType?? DeviceComponentTypeEnum.All;
                var compenttypeName = compnentlist.FirstOrDefault(x => x.ID == tempata[i].CompentId)?.ComponentType.GetDescription();
                tempata[i].ComponentName = compname;
                tempata[i].CompentTypeEnum = compenttypeenum;
                tempata[i].CompentTypeName = compenttypeName;
                var deviceStatusDesc = StatusEnumHelper.GetDescription(tempata[i].Status);           
                tempata[i].CompentStatusDesc = deviceStatusDesc;
                tempata[i].DeviceName = devicemodel.DeviceName ?? "未知设备";


            });
        }


        public override void InitDataBaseName()
        {
            DataBaseName = DatabaseEnum.CollectionDataResult.ToString();
        }

        public int AddData(List<CompentDataStatistics> li)
        {
           return  this.Client.Insertable(li).ExecuteCommand();
        }
    }
}
